package com.mx.tool.form.window.zh;

import cn.hutool.core.convert.Convert;
import com.github.promeg.pinyinhelper.Pinyin;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.util.Locale;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-10 14:22
 * @Description:
 */
public class PinyinWindow {
    private JPanel jPanel;
    private JTextArea contentArea;
    private JTextArea showArea;
    private JButton jButton;
    private JComboBox<String> fgBox;
    private JButton clearButton;
    private JCheckBox caseBox;

    public PinyinWindow() {
        jButton.addActionListener(e -> {
            String content = contentArea.getText();
            if (StringUtils.isNotBlank(content)) {
                String fg = Convert.toStr(fgBox.getSelectedItem());
                String result;
                switch (fg) {
                    case "无分隔符":
                        result = Pinyin.toPinyin(content, "");
                        break;
                    case "空格分隔":
                        result = Pinyin.toPinyin(content, " ");
                        break;
                    case "'-'中划线分隔":
                        result = Pinyin.toPinyin(content, "-");
                        break;
                    case "'_'下划线分隔":
                        result = Pinyin.toPinyin(content, "_");
                        break;
                    case "'.'分隔":
                        result = Pinyin.toPinyin(content, ".");
                        break;
                    case "'|'竖线分隔":
                        result = Pinyin.toPinyin(content, "|");
                        break;
                    default:
                        result = Pinyin.toPinyin(content, "");
                        break;
                }
                if(caseBox.isSelected()){
                    result = result.toUpperCase(Locale.ROOT);
                }else{
                    result = result.toLowerCase(Locale.ROOT);
                }
                showArea.setText(result);
            }
        });
        clearButton.addActionListener(e -> {
            contentArea.setText("");
            showArea.setText("");
        });
    }

    public JPanel getContent() {
        return jPanel;
    }
}
